import urllib

import execjs
import requests


headers = {
    "accept": "application/json, text/plain, */*",
    "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,ar;q=0.7",
    "cache-control": "no-cache",
    "pragma": "no-cache",
    "priority": "u=1, i",
    "referer": "https://www.douyin.com/jingxuan/search/NBA%E6%9C%80%E6%96%B0%E4%BA%A4%E6%98%93?aid=c1e9bee0-2f14-49c2-a1d8-bcdc8d25a261&type=general",
    "sec-ch-ua": "\"Not)A;Brand\";v=\"8\", \"Chromium\";v=\"138\", \"Microsoft Edge\";v=\"138\"",
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": "\"Windows\"",
    "sec-fetch-dest": "empty",
    "sec-fetch-mode": "cors",
    "sec-fetch-site": "same-origin",
    "uifid": "d4579b5b1721ffdd22d8a6ff378781159e3b4e5a1248a83fc8c708ec2606b705c55e418f00ac63d7cf11eec8e42b3216af21d0a56129cec84cda8c1492358f3d9587fb918391e8fe9813c4b75b6689535818f807e9c461b66d15d64b6461a1efa4219cda0c11670520e24d0911c010ae468b1b0e6105bd581b71e1e3875c86b36ba7095a8199d595f6194b81728a42739e90736ee95ff577f1723ea42fb8bf98",
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36 Edg/138.0.0.0"
}
cookies = {
    "__security_mc_1_s_sdk_crypt_sdk": "fe3152a7-43c4-80cc",
    "__security_mc_1_s_sdk_cert_key": "a5ec43a2-437a-82fc",
    "__security_mc_1_s_sdk_sign_data_key_web_protect": "435f4546-4b48-8aa9",
    "device_web_cpu_core": "16",
    "device_web_memory_size": "8",
    "architecture": "amd64",
    "volume_info": "%7B%22volume%22%3A0.6%7D",
    "s_v_web_id": "verify_mck6cfxj_BH5FR6d8_vbUY_4ha9_9Ori_w7xS6BYwmsNV",
    "odin_tt": "d892d4ccaa608cecd6b1a1261a1718f5960e1eeacca3f54056103e89bfe9781993cdebdda34b0c34ade8ebbf9ea6d8462eefffacb840a855dbc738273552bf5b8aa162fa0e1f9f8e61229130c43c73d6",
    "xgplayer_user_id": "633757564182",
    "live_use_vvc": "%22false%22",
    "__ac_signature": "_02B4Z6wo00f01R-CxiAAAIDB8SGbaefHL7kfosKAAC-Gb7",
    "enter_pc_once": "1",
    "UIFID_TEMP": "d4579b5b1721ffdd22d8a6ff378781159e3b4e5a1248a83fc8c708ec2606b705e7026a41a256323e32a331dd71369cb1c4b92016e36dfdf5fcd9c7539a7015c906515b4e4a96cee4a9163a5796298d4f",
    "douyin.com": "",
    "hevc_supported": "true",
    "IsDouyinActive": "true",
    "dy_swidth": "1920",
    "dy_sheight": "1080",
    "stream_recommend_feed_params": "%22%7B%5C%22cookie_enabled%5C%22%3Atrue%2C%5C%22screen_width%5C%22%3A1920%2C%5C%22screen_height%5C%22%3A1080%2C%5C%22browser_online%5C%22%3Atrue%2C%5C%22cpu_core_num%5C%22%3A16%2C%5C%22device_memory%5C%22%3A8%2C%5C%22downlink%5C%22%3A10%2C%5C%22effective_type%5C%22%3A%5C%224g%5C%22%2C%5C%22round_trip_time%5C%22%3A50%7D%22",
    "strategyABtestKey": "%221751353011.997%22",
    "is_dash_user": "1",
    "stream_player_status_params": "%22%7B%5C%22is_auto_play%5C%22%3A0%2C%5C%22is_full_screen%5C%22%3A0%2C%5C%22is_full_webscreen%5C%22%3A0%2C%5C%22is_mute%5C%22%3A1%2C%5C%22is_speed%5C%22%3A1%2C%5C%22is_visible%5C%22%3A1%7D%22",
    "ttwid": "1%7CVdGDY2q6ockgwawSYBKgGj8p67yQk1IkLiW--KwO8F8%7C1751353013%7C4c7165b66a0143224ba96e7eb0ca799286496d711c6c7d8bcb20a609572382f7",
    "biz_trace_id": "36b8b897",
    "passport_csrf_token": "fa5ac645ccf01fb1909160ed122a172c",
    "passport_csrf_token_default": "fa5ac645ccf01fb1909160ed122a172c",
    "fpk1": "U2FsdGVkX1+QXY9Pk3IDO+yuh5yDh7L3YovWnhOywkhIs49Sv/6FAS0hcfCd5Jt6jRkSShzXaOpL/OEQPogEnA==",
    "fpk2": "d2ad6785d256851dd366703bdc61aa61",
    "bd_ticket_guard_client_data": "eyJiZC10aWNrZXQtZ3VhcmQtdmVyc2lvbiI6MiwiYmQtdGlja2V0LWd1YXJkLWl0ZXJhdGlvbi12ZXJzaW9uIjoxLCJiZC10aWNrZXQtZ3VhcmQtcmVlLXB1YmxpYy1rZXkiOiJCTmlYaGVyTEQxeXhFL1ZuSlVjRWhNaFRBdVUyQ0M2SElNMWtZMW0raUUrRnIyTk5BWnlTSEp3V1JFMkI0QWdPSU1JTC9HWjh6S3hzeW5US0tyL29TdFk9IiwiYmQtdGlja2V0LWd1YXJkLXdlYi12ZXJzaW9uIjoyfQ%3D%3D",
    "bd_ticket_guard_client_web_domain": "2",
    "home_can_add_dy_2_desktop": "%221%22",
    "xg_device_score": "7.90435294117647",
    "download_guide": "%221%2F20250701%2F0%22",
    "UIFID": "d4579b5b1721ffdd22d8a6ff378781159e3b4e5a1248a83fc8c708ec2606b705c55e418f00ac63d7cf11eec8e42b3216af21d0a56129cec84cda8c1492358f3d9587fb918391e8fe9813c4b75b6689535818f807e9c461b66d15d64b6461a1efa4219cda0c11670520e24d0911c010ae468b1b0e6105bd581b71e1e3875c86b36ba7095a8199d595f6194b81728a42739e90736ee95ff577f1723ea42fb8bf98",
    "WallpaperGuide": "%7B%22showTime%22%3A1751359249279%2C%22closeTime%22%3A0%2C%22showCount%22%3A1%2C%22cursor1%22%3A10%2C%22cursor2%22%3A2%7D"
}

params = {
    "query": "NBA最新交易",
    "business_id": "30125",
    "aid": "6383",
    "app_name": "douyin_web",
    "pd": "aweme_general",
    "update_version_code": "170400",
    "pc_client_type": "1",
    "pc_libra_divert": "Windows",
    "support_h265": "1",
    "support_dash": "0",
    "cpu_core_num": "16",
    "version_code": "170400",
    "version_name": "17.4.0",
    "cookie_enabled": "true",
    "screen_width": "1920",
    "screen_height": "1080",
    "browser_language": "zh-CN",
    "browser_platform": "Win32",
    "browser_name": "Edge",
    "browser_version": "138.0.0.0",
    "browser_online": "true",
    "engine_name": "Blink",
    "engine_version": "138.0.0.0",
    "os_name": "Windows",
    "os_version": "10",
    "device_memory": "8",
    "platform": "PC",
    "downlink": "10",
    "effective_type": "4g",
    "round_trip_time": "50",
    "webid": "7522003828189218347",
    "uifid": "d4579b5b1721ffdd22d8a6ff378781159e3b4e5a1248a83fc8c708ec2606b705c55e418f00ac63d7cf11eec8e42b3216af21d0a56129cec84cda8c1492358f3d9587fb918391e8fe9813c4b75b6689535818f807e9c461b66d15d64b6461a1efa4219cda0c11670520e24d0911c010ae468b1b0e6105bd581b71e1e3875c86b36ba7095a8199d595f6194b81728a42739e90736ee95ff577f1723ea42fb8bf98",
    "verifyFp": "verify_mck6cfxj_BH5FR6d8_vbUY_4ha9_9Ori_w7xS6BYwmsNV",
    "fp": "verify_mck6cfxj_BH5FR6d8_vbUY_4ha9_9Ori_w7xS6BYwmsNV",
    "msToken": "Co99RjYYOkt4aADOBD4t4rbYtefvLvkOo9fgCKM3XYFL7gV9zB-RExcRV_U65CG5Z1q_9sv6GTRRBvxjuvsPl5CL50w6YOksuJ46umaREOzSBdNTQS_ame4qBY9PBrp0WysZtTZDkZzwrDBYKVpui2weJ2ep6dgeh15Hew2xAZtjy1V7WBvOZw==",
    "a_bogus": "E705hwtJDdmfFd/GYcx2S31lG7fArPWyDaT2WzrTHOKnT1lPERNbwcbgrooIJEr5FSBhhopHLVsAOfdcKtXhZKnpomkvShh6TTVC9Wfo8qidbzvkgqb8ezWzzwBxUcTqlQVUiAW60Urq6nVAkNdD/Bl9SKoFQRuBF1xWkMucE9sXZ0gAEpc1PQG2O7iqPD=="
}
fp = execjs.compile(open("verifyFp.js",'r').read()).call("fp")

params['verifyFp'] = fp
params['fp'] = fp
print(fp)
encode_params_string = urllib.parse.urlencode(params,safe="=")
url = 'https://www.douyin.com/aweme/v1/web/api/suggest_words/?' + encode_params_string
print(url)
a_bogus = execjs.compile(open("main.js",'r',encoding="utf-8").read()).call("get_abgous",url)
print(a_bogus)

params['a_bogus'] = a_bogus
response = requests.get(url, headers=headers, cookies=cookies, params=params)

print(response.text)
print(response)